package com.yunkaweilai.android.activity.lanya;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.StoreInfoModel;
import com.yunkaweilai.android.net.Glide.GlideUtils;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/5/2.
 */

public class StoreInfoActivity extends BaseActivity {

    @BindView(R.id.titlebar_iv_left)
    ImageView titlebarIvLeft;
    @BindView(R.id.titlebar_tv_left)
    TextView titlebarTvLeft;
    @BindView(R.id.titlebar_tv)
    TextView titlebarTv;
    @BindView(R.id.titlebar_iv_right_2)
    ImageView titlebarIvRight2;
    @BindView(R.id.titlebar_iv_right)
    ImageView titlebarIvRight;
    @BindView(R.id.titlebar_tv_right)
    TextView titlebarTvRight;
    @BindView(R.id.id_view_underline)
    View idViewUnderline;
    @BindView(R.id.rl_titlebar)
    RelativeLayout rlTitlebar;
    @BindView(R.id.id_img_logo)
    ImageView idImgLogo;
    @BindView(R.id.id_tv_name)
    TextView idTvName;
    @BindView(R.id.id_tv_type)
    TextView idTvType;
    @BindView(R.id.id_tv_time)
    TextView idTvTime;
    @BindView(R.id.id_tv_info)
    TextView idTvInfo;
    @BindView(R.id.id_tv_member_num)
    TextView idTvMemberNum;
    @BindView(R.id.id_tv_sms)
    TextView idTvSms;
    @BindView(R.id.content_view)
    LinearLayout contentView;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    @BindView(R.id.id_llayout_add_time)
    LinearLayout idLlayoutAddTime;
    @BindView(R.id.id_tv_store_code)
    TextView idTvStoreCode;
    private TitleBuilder titleBuilder;
    private Gson gson = new Gson();
    private StoreInfoModel storeInfoModel;
    private LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_info);
        ButterKnife.bind(this);
        titleBuilder = new TitleBuilder(mAc).setTitleText("店铺资料").setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        if (BaseApplication.getAppRuleModel().isIs_super()) {
            titleBuilder.setRightText("修改").setRightOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivityForResult(new Intent(mAc, UpdateStoreInfoActivity.class), 101);
                }
            });
        }
        inflater = this.getLayoutInflater();
        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        initHttp();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 101) {
                idMultipleStatusView.showLoading();
                initHttp();
            }
        }
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.STORE_INFO)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            storeInfoModel = gson.fromJson(json, StoreInfoModel.class);
                            initView();
                        } else {
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showError();
                            }
                        }

                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView != null) {
                            idMultipleStatusView.showError();
                        }
                    }
                });
    }

    private void initView() {
        if (idMultipleStatusView != null) {
            idMultipleStatusView.showContent();
        }
        StoreInfoModel.DataBean.InfoBean info = storeInfoModel.getData().getInfo();
        GlideUtils.loadNoPlaceImage(mAc, info.getStore_logo(), idImgLogo);
        idTvName.setText(info.getStore_name() + "");
        idTvType.setText(info.getIndustry_name() + "");
        idTvTime.setText(info.getEnter_time() + "");
        idTvInfo.setText(info.getIs_sub_store() + "");
        idTvMemberNum.setText(info.getMember_count() + "");
        idTvSms.setText(info.getSms_count() + "");
        idTvStoreCode.setText(info.getStore_num() + "");
//        idLlayoutAddTime.removeAllViews();
//        for (int i = 0; i < info.getValidateStore().size(); i++) {
//            StoreInfoModel.DataBean.InfoBean.ValidateStoreBean validateStoreBean = info.getValidateStore().get(i);
//            LinearLayout view = (LinearLayout) inflater
//                    .inflate(R.layout.view_store_time, null);
//            ((TextView) view.findViewById(R.id.id_tv_left)).setText(validateStoreBean.getVersion_str() + "有效期");
//            ((TextView) view.findViewById(R.id.id_tv_right)).setText(validateStoreBean.getDeadline_time_str() + "");
//            idLlayoutAddTime.addView(view);
//        }

    }
}
